package cn.codesea.housework_user.ui;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;

import java.text.ParseException;
import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.entity.Order;
import cn.codesea.housework_user.entity.Service;
import cn.codesea.housework_user.entity.User;
import cn.codesea.housework_user.utils.SPUtil;

public class ServiceActivity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.order_tv)
    TextView order_tv;

    Service service;
//    User user;

    @BindView(R.id.desc_tv)
    TextView desc_tv;
    @BindView(R.id.type_tv)
    TextView type_tv;

    @BindView(R.id.start_time_tv)
    TextView start_time_tv;
    @BindView(R.id.end_time_tv)
    TextView end_time_tv;
    @BindView(R.id.cost_tv)
    TextView cost_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);
        ButterKnife.bind(this);
        init();
    }

    private void init(){
        initData();
        initView();
    }

    private void initData(){
        service = (Service) getIntent().getSerializableExtra("service");
//        user = SPUtil.getUser(ServiceActivity.this);

    }

    private void initView() {
        // 初始化 toolbar
        toolbar.setTitle(service.getName());
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        desc_tv.setText(service.getDesc());
        type_tv.setText(service.getName()+" "+service.getPrice()+"/"+service.getServicetype());

    }

    @OnClick(R.id.order_tv)
    public void order_tv(){

        Order order = new Order();
        order.setServiceid(service.getId());
//        order.setUserid(user.getUserid());
//        order.setOlder();
//        order.setWorkerid();
        order.setStatus(0);
        order.setService_starttime(start_time_tv.getText().toString());
        order.setService_endtime(end_time_tv.getText().toString());
        order.setCost(cost_tv.getText().toString());

        startActivity(new Intent(ServiceActivity.this,RYTJListActivity.class).putExtra("order",order));
    }

    @OnClick(R.id.start_time_tv)
    public void start_time_tv(){
        new DatePickerDialog(ServiceActivity.this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                        monthOfYear += 1;
                        start_time_tv.setText(year+"-"+monthOfYear+"-"+dayOfMonth);

                        if (!"".equals(end_time_tv.getText().toString())){
                            countCost();
                        }

                    }
                }, Calendar.getInstance().get(Calendar.YEAR), Calendar
                .getInstance().get(Calendar.MONTH), Calendar
                .getInstance().get(Calendar.DATE)).show();

    }

    @OnClick(R.id.end_time_tv)
    public void end_time_tv(){
        new DatePickerDialog(ServiceActivity.this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                        monthOfYear += 1;
                        end_time_tv.setText(year+"-"+monthOfYear+"-"+dayOfMonth);

                        if (!"".equals(start_time_tv.getText().toString())){
                            countCost();
                        }
                    }
                }, Calendar.getInstance().get(Calendar.YEAR), Calendar
                .getInstance().get(Calendar.MONTH), Calendar
                .getInstance().get(Calendar.DATE)).show();
    }

    /**
     * 计算费用
     */
    private void countCost(){
        String start_time = start_time_tv.getText().toString();
        String end_time = end_time_tv.getText().toString();

        long days = getDaySub(start_time,end_time) + 1;

        long cost = 0;
        if ("天".equals(service.getServicetype())){
            cost = Long.parseLong(service.getPrice()) * days;
        }else if("次".equals(service.getServicetype())){
            cost = Long.parseLong(service.getPrice());
        }else if("月".equals(service.getServicetype())){
            long per = Long.parseLong(service.getPrice())/30;
            cost = per * days;
        }

        cost_tv.setText(cost+"元");
    }

    /**
     * <li>功能描述：时间相减得到天数
     * @param beginDateStr
     * @param endDateStr
     * @return
     * long
     * @author Administrator
     */
    public static long getDaySub(String beginDateStr,String endDateStr)
    {
        long day=0;
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
        java.util.Date beginDate;
        java.util.Date endDate;
        try
        {
            beginDate = format.parse(beginDateStr);
            endDate= format.parse(endDateStr);
            day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000);
            //System.out.println("相隔的天数="+day);
        } catch (ParseException e)
        {
            e.printStackTrace();
        }
        return day;
    }

}
